package com.example.back_code.controller;

import com.example.back_code.entity.po.Paper;
import com.example.back_code.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student/paper")
public class StudentPaperController {

    @Autowired
    private PaperService paperService;

    // 查询本人所有论文
    @GetMapping("/list")
    public List<Paper> list(@RequestParam Long userId) {
        return paperService.lambdaQuery().eq(Paper::getUserId, userId).list();
    }

    // 添加论文
    @PostMapping("/add")
    public boolean add(@RequestParam Long userId, @RequestBody Paper paper) {
        paper.setUserId(userId);
        paper.setStatus(0); // 默认待审核
        return paperService.save(paper);
    }

    // 修改论文（仅待审核可改）
    @PutMapping("/update/{id}")
    public boolean update(@RequestParam Long userId, @PathVariable Long id, @RequestBody Paper paper) {
        Paper dbPaper = paperService.getById(id);
        if (dbPaper == null || !dbPaper.getUserId().equals(userId) || dbPaper.getStatus() != 0) {
            return false;
        }
        paper.setId(id);
        paper.setUserId(userId);
        paper.setStatus(0);
        return paperService.updateById(paper);
    }

    // 删除论文（仅待审核可删）
    @DeleteMapping("/delete/{id}")
    public boolean delete(@RequestParam Long userId, @PathVariable Long id) {
        Paper dbPaper = paperService.getById(id);
        if (dbPaper == null || !dbPaper.getUserId().equals(userId) || dbPaper.getStatus() != 0) {
            return false;
        }
        return paperService.removeById(id);
    }
} 